/**
 * Created by Administrator on 2018-08-05.
 * 次日航班计划
 */

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<this.length;i++){
        for(var j=i+1; j<this.length;j++){
            if(a[i] === a[j])
                a.splice(j, 1);
        }
    }
    return a;
};

define(function (require,exports,module) {
    var sTpl = require("./secProcessControl.html");
    require("../../lib/fullcalendar/moment.min.js");
    var socket=require("../util/socketServer.js");
    require("./secProcessControl.css");
    var VueComponent = Vue.extend({
        template: sTpl,
        components:{
            'messages':socket
        },
        methods:{
            queryAppointment :function(){
                this.urls.url=this.urls.initurl+"&importOrExport=进港"+"&flightInfoId="+this.form.flightInfo.flightInfoId+
                    "&estimatedTime="+this.nextDay;
                this.urls.url2=this.urls.initurl+"&importOrExport=出港"+"&flightInfoId="+this.form.flightInfo.flightInfoId+
                    "&estimatedTime="+this.nextDay;
                var $this = this;
                axios.get(this.urls.url).then(function (response){
                    $this.importData=response.data;
                });
                axios.get(this.urls.url2).then(function (response){
                    $this.exportData=response.data;
                });
            },
            nodeCompleted:function (node,nodes) {
                if(nodes){
                    var result= false;
                    nodes.forEach(function (value) {
                        if( node.nodeName == value.nodeName){
                            result = true;
                        }
                    })
                    return result;
                }
                return false;

            },
            nodeComplete:function (scope,node) {
                var $this = this;

                this.$confirm('确定 '+node.nodeName+' 节点已完成', '确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    node.flightPlanePo = JSON.parse(JSON.stringify(scope.row));
                    node.flightPlanePo.courseNodePos=[];
                    node.dataSources="PC";
                    axios.post(basepath+"/api/flightPlane/nodeCompleted",node).then(function (response) {
                        $this.$notify({tittle:" 节点已完成",message:node.nodeName+" 节点 已完成"});
                        for(var i=0;i<$this.importData.length;i++){
                            if(node.flightPlanePo.planeId==$this.importData[i].planeId){
                                var b=$this.importData[i].courseNodePos.find(function (val) {
                                    return val.nodeId==a.nodeId;
                                })
                                if(!b){
                                    $this.importData[i].courseNodePos.push(response.data);
                                }
                            }
                        }
                        for(var i=0;i<$this.exportData.length;i++){
                            if(node.flightPlanePo.planeId==$this.exportData[i].planeId){
                                var b=$this.exportData[i].courseNodePos.find(function (val) {
                                    return val.nodeId==a.nodeId;
                                })
                                if(!b){
                                    $this.exportData[i].courseNodePos.push(response.data);
                                }

                            }
                        }

                    }).catch(function(_) {

                    });
                })

            },
            rowClass:function (row,a) {
                return "";

            },



            examineAppointment:function(scope){
                this.$refs.enters.formData=scope.row;
                this.$refs.enters.parentData.showOrEdit='show';
                this.$refs.enters.parentData.dataCallBack=function(data){
                    scope.row=data;
                }
                $("#panel-activer").click();
            },

            editFlightPlane:function(scope){
                this.flightPlaneEdit=true;
                this.enterData.editOrAdd='edit';
                var $this= this;
                this.enterData.openEdit=function (a) {
                    a(JSON.parse(JSON.stringify(scope.row)));
                }
                this.enterData.editCallback=function (val) {
                    $this.flightPlaneEdit=false;
                    $this.queryAppointment();
                }

            },
            editAppointment:function (scope) {
                this.appointmentEdit=true;
                this.appointment =scope.row;
                this.handleSelect(scope.row.flightInfo.flightInfoId);
                this.currentAppointmentScope = scope;
            },
            handleSelect:function (item) {
                this.flightInfo =this.planes.find(function (val) {
                    return val.flightInfoId ==item;
                })
                console.log(item);
            },
            pickExpectDate:function (val) {
                if(val==null){
                    this.appointment.appoDate = "";
                }else{
                    this.appointment.appoDate = moment(val).format("YYYY-MM-DD");;
                }

            },
            pickEstimatedTime:function (val) {
                if(val==null){
                    this.appointment.estimatedTime = "";
                }else{
                    this.appointment.estimatedTime = moment(val).format("YYYY-MM-DD");;
                }

            },
            querySearchPlace:function(queryString, cb) {
                var restaurants = this.citys;
                var results = queryString ? restaurants.filter(function(restaurant){
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                }) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            confirmEditAppointment :function(){
                var $this = this ;
                this.$refs["appointment"].validate(function(valid) {
                    if (valid) {
                        var data = JSON.parse(JSON.stringify($this.appointment));
                        if(!data.agreement||!data.agreement.contractManageId){
                            data.agreement=null;
                        }
                        axios.post(basepath+'/api/appointment/saveAppointmentAndFlight',data)
                            .then(function (response) {
                                $this.appointmentEdit=false;
                                $this.$notify({
                                    title: '修改成功',
                                    message: '修改成功'
                                });
                            });
                        return true;
                    } else {
                        $this.$notify({
                            title: '校验失败',
                            message: '请检查修改字段'
                        });
                    }
                });
            }
        },

        mounted:function() {

        }
        ,data:function(){
            var date = moment().format("YYYY-MM-DD");
            var $this = this;
            return {
                nextDay:date,
                messageComponent:{messageType:"PROCESS_CONTROL"},
                nodes:[],
                flightPlaneEdit:false,
                flightPlaneExcel :false,
                currentAppointmentScope:{},
                diawidth:"750px",
                form: {
                    appoUnit: '',
                    flightInfo:{flightInfoId:""},
                    startDate:'',
                    endDate:'',
                    unExamine:false,
                    appoCancle:false,
                    examinePass:false,
                    examineCancle:false,
                    appoPhoneNum:'',
                    type:""
                },
                enterData:{
                    editOrAdd:'add',
                    unShowExcel:"true",
                    planeProperty:'批文',
                    onlyTime:true
                },
                enterOpen:true,
                tableData:[],
                importData:[],
                exportData:[],
                units: [],
                urls:{
                    url:basepath+"/api/flightPlane/findTodayPlane?importOrExport=进港",
                    url2:basepath+"/api/flightPlane/findTodayPlane?importOrExport=出港",
                    initurl:basepath+"/api/flightPlane/findTodayPlane?order=estimatedTime",
                },
                formData: [],
                appointment:{flightInfo:{flightInfoId:""}},
                flightInfo:{},
                planes:[],
                pickerOptions2: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 7).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 30).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 90).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                }
            }
        },
        created:function (){

            var $this = this;

            axios.get(basepath+"/api/sysSetting/findCnb"
            ).then(function (response) {
                $this.nodes=response.data;
                $this.nodes.sort(function (val1,val2) {
                    return val1.nodeSeq>val2.nodeSeq?1:-1;
                })
            });
            this.queryAppointment();
            axios.get(basepath+"/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
                $this.planesMap={};
                $this.planes.forEach(function(plane){
                    plane.value= plane.flightNum;
                    $this.planesMap[plane.flightNum] = plane;
                })
            })
            messages.$on(this.messageComponent.messageType,function (event) {
                var a=JSON.parse(event.data);
                for(var i=0;i<$this.importData.length;i++){
                    if(a.flightPlanePo.planeId==$this.importData[i].planeId){
                        var b=$this.importData[i].courseNodePos.find(function (val) {
                            return val.nodeId==a.nodeId;
                        })
                        if(!b){
                            $this.importData[i].courseNodePos.push(a);
                        }
                    }
                }
                for(var i=0;i<$this.exportData.length;i++){
                    if(a.flightPlanePo.planeId==$this.exportData[i].planeId){
                        var b=$this.exportData[i].courseNodePos.find(function (val) {
                            return val.nodeId==a.nodeId;
                        })
                        if(!b){
                            $this.exportData[i].courseNodePos.push(a);
                        }

                    }
                }
                // $this.currentFlight =JSON.parse(JSON.stringify($this.currentFlight));

            });
        },
        activated:function () {
            messages.$emit('change','保障进程监控');
        }
    });
    module.exports = VueComponent;
})
